package com.qfedu.provider.listener;

import com.qfedu.config.RedisKeyConfig;
import com.qfedu.config.RocketmqConfig;
import com.qfedu.provider.aigc.AigcUtil;
import com.qfedu.provider.entity.DbDynamicLog;
import com.qfedu.provider.entity.DbDynamics;
import com.qfedu.provider.entity.DbProductType;
import com.qfedu.provider.entity.DbUser;
import com.qfedu.provider.mapper.DbDynamicLogMapper;
import com.qfedu.provider.mapper.DbDynamicsMapper;
import com.qfedu.provider.mapper.DbProductTypeMapper;
import com.qfedu.provider.mapper.DbUserMapper;
import lombok.RequiredArgsConstructor;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 千锋Java,为你而生！
 *
 * @Author: Feri
 * @Date:2024/11/21 16:15
 * @Modified:
 */
@Component
@RequiredArgsConstructor
@RocketMQMessageListener(topic = RocketmqConfig.TOPIC_MOODS,consumerGroup = "moodconsumer")
public class MoodAuditListener implements RocketMQListener<Integer> {
    private final DbDynamicsMapper dao;
    private final DbDynamicLogMapper dbDynamicLogMapper;
    private final DbProductTypeMapper dbProductTypeMapper;
    private final DbUserMapper dbUserMapper;
    private final StringRedisTemplate stringRedisTemplate;

    private static final String ROLE="你一名动态内容审核官，专门审核内容是否合规合法，如果内容涉及到色情、暴力、政治等敏感信息，需要进行反馈";
    @Override
    public void onMessage(Integer id) {
        //1.查询动态
        DbDynamics mood=dao.selectById(id);
        DbProductType dbProductType = dbProductTypeMapper.selectById(mood.getTypeId());
        String type = dbProductType.getType();
        DbUser dbUser = dbUserMapper.selectById(mood.getUserId());
        String nickName = dbUser.getNickName();
        //2.调用AIGC内容进行审核
        String result= AigcUtil.createQuestion(ROLE,mood.getDescription()+"。判断这句话，是否合规，只要结果不要其他的");
        if(StringUtils.hasLength(result)){
            if(!result.contains("不合规")){
                //合规
                mood.setStatus(1);
                mood.setUpdateTime(new Date());
                dao.updateById(mood);
                //涉及任务中心 奖励
                dbDynamicLogMapper.insert(new DbDynamicLog(mood.getUserId(),type,mood.getId()+"","审核通过ip",nickName+"发布一条新动态,审核通过！"));


            }else{
                //不合格
                mood.setStatus(2);
                mood.setUpdateTime(new Date());
                dao.updateById(mood);

                dbDynamicLogMapper.insert(new DbDynamicLog(mood.getUserId(),type,mood.getId()+"","审核不通过ip",nickName+"发布一条新动态,审核未通过！"));
            }
        }
    }
}
